package core.encoder.lianwei;

import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Value;

public class PIIDataAESEncoder  {

    private final AESEncoder aesEncoder;

    public PIIDataAESEncoder(@Value("${pii-data.secret-key}") String secretKey) {
        this.aesEncoder = new AESEncoder(secretKey);
    }


    public String encode(String plainText) {
        return aesEncoder.encode(plainText);
    }


    public String decode(String encodedText) {
        try {
            return aesEncoder.decode(encodedText);
        } catch (Exception e) {
            return encodedText;
        }
    }


    public String decodeMobile(String encodedText) {
        String text = decode(encodedText);
        if (StrUtil.isBlank(text)) {
            return text;
        }

        if (text.length() < 7) {
            return text;
        }

        return text.substring(0, 3).concat("****").concat(text.substring(7));
    }


    public String decodePhone(String encodedText) {
        String text = decode(encodedText);
        if (StrUtil.isBlank(text)) {
            return text;
        }

        if (text.length() < 8) {
            return text;
        }

        return text.substring(0, text.length() - 4).concat("****");
    }



}